<?php
/***
 * 描述：产品分类管理[管理员权限]
 * @category		Admin		
 * @package			Action			
 * @subpackage			
 * @author			wanght <whtaok@gmail.com>
 * @since			2013-4-6
 * @version			$Id: CateManageAction.class.php 48 2013-11-08 13:57:40Z whtaok@gmail.com $
 */
class CateManageAction extends AdminAction {
	private $savePath = '';
	/**
	 *
	 * @access		protected
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	protected  function _initialize() {
		parent::_initialize();
		$this->usingModel="ProductCate";
		$this->savePath = C('REALPATH_SHOP').'/cate_code2';
	}
	/**
	 * 产品分类首页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function indexAct($cate_id=0) {
		//1.获得所有商品分类
		$mod	=	new ProductCateModel();
		$mod->validForWhere('1,2,3');
		$cate	=	$mod->select();
		//2.生成菜单树型数组(common.php->list_to_tree)
		$cateTree = list_to_tree($cate);
		
		$this->assign("cateTree",$cateTree);
		
		//3.生成分类瀑布数组(common.php->list_to_treefalls)
		$cateTreefalls = list_to_treefalls($cateTree);
		if($cate_id){
			//3.1显示指定菜单ID的菜单列表
			foreach ($cateTreefalls as $key=>$value){
				//1.查询的菜单ID == 当前遍历的菜单ID  显示一层菜单 $key == $menu_id
				//2.查询的菜单ID == 当前遍历的菜单PID 显示二层菜单 $value['menu_pid'] == $menu_id
				if($cate_id == $key || $cate_id == $value['pid']){
					$cateList[$key] = $value;
				}
			}
		}else{
			//3.1显示全部的查询菜单列表
			$cateList = $cateTreefalls;
		}
		unset($cateTreefalls);
		unset($cateTree);
		//4.设置页面变量
		
		$this->assign("cateId",$cate_id);
		$this->assign("cateList",$cateList);
		
		$this->display('index');
	}
	
	/**
	 * 显示添加页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showAddAct($cate_pid=0) {
		if($cate_pid){
			//子菜单：查询父分类信息
			$mod =	new ProductCateModel();
			$pcate = $mod->find($cate_pid);
			if(!$pcate){
				exit($this->dialog(STATUS_ERROR,'父分类不存在，请重新操作'));
			}
		}else{
			//顶级菜单：初始化父分类信息
			$pcate['id']=$cate_pid;
			$pcate['title']='顶级分类';
		}
		$this->assign("pcate",$pcate);
		exit($this->fetch("add"));
	}
	
	/**
	 * 添加操作
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doAddAct() {
		F('productcate_tree',NULL);
		F_CACHE($this->savePath);
		$mod=new ProductCateModel();
		
		if(!$mod->create()){
			$this->error($mod->getError());
		}
		$mod->add();
		$this->success('添加成功', U('index'));
	}
	
	/**
	 * 显示编辑页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showUpdAct($cate_id) {
		//查询当前菜单信息
		$mod =	new ProductCateModel();
		$cate = $mod->find($cate_id);
		
		//是否可以修改父级分类
		if($cate['pid']){
			//查询父类菜单信息
			$pcate = $mod->find($cate['pid']);
			//如果非顶级菜单，则查询和父级菜单同级的菜单列表
			$pcateList = $mod->where(array('pid'=>$pcate['pid']))->select();
		}else{
			//如果是顶级菜单，则不可以修改父类菜单
			$pcate = array(
				'id'=>'0',
				'title'=>'顶级分类',
			);
			$pcateList[]=$pcate;
		}
		
		//设置页面变量
		$this->assign("cate",$cate);
		$this->assign("pcate",$pcate);
		$this->assign("pcateList",$pcateList);
		exit($this->fetch("upd"));
	}
	
	/**
	 * 编辑操作
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doUpdAct($id) {
		F('productcate_tree',NULL);
		F_CACHE($this->savePath);
		$mod=new ProductCateModel();
		if(!$mod->create()){
			$this->error($mod->getError());
		}
		$mod->save();
		$this->success('保存成功', U('index'));
	}
	
	/**
	 * 删除操作
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doDelAct($id) {
		F('productcate_tree',NULL);
		F_CACHE($this->savePath);
		$mod = new ProductCateModel();
		$mod->idForWhere($id);
		$mod->setField('is_valid','4');
		$this->success('删除成功', U('index'));
	}
	
	/**
	 * 删除缓存
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function removeCacheAct(){
		F('productcate_tree',NULL);
		F_CACHE($this->savePath);
		$this->ajaxReturn("缓存删除成功");
	}

}

?>